//
//  ZYWorkModeSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/5.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYWorkModeSelectView.h"
#import "ZYModeTypeModel.h"
#import "UIButton+ImageTitleSpacing.h"

@interface ZYWorkModeSelectView()
@property(nonatomic,strong) NSMutableArray<UIView *> *bgViewArray;
@property(nonatomic,strong) NSMutableArray<UIButton *> *buttonArray;
@property(nonatomic,strong) NSArray<ZYModeTypeModel *> *dataArray;
@property(nonatomic,assign) BOOL isAlterSelectView;
@property(nonatomic,strong) UIView *bgView;
@end

@implementation ZYWorkModeSelectView

-(instancetype)initWithFrame:(CGRect)frame modelArray:(NSArray<ZYModeTypeModel *> *)modelArray{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWithModelArray:modelArray];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupWorkModeSelectView];
    }
    return self;
}


-(void)setupWorkModeSelectView{
    NSArray<NSNumber *> *modeTypeArray = @[[NSNumber numberWithUnsignedInteger:ZYWorkModeTypeRefrigeration],
                                           [NSNumber numberWithUnsignedInteger:ZYWorkModeTypeDehumidify],
                                           [NSNumber numberWithUnsignedInteger:ZYWorkModeTypeOffer],
                                           [NSNumber numberWithUnsignedInteger:ZYWorkModeTypeHeating],
                                           [NSNumber numberWithUnsignedInteger:ZYWorkModeTypeAuto]];
    NSArray<NSString *> *imageNameArray = @[@"ic_work_mode_type_refrigeration_unselected",
                                            @"ic_work_mode_type_dehumidify_unselected",
                                            @"ic_work_mode_type_offer_unselected",
                                            @"ic_work_mode_type_heating_unselected",
                                            @"ic_work_mode_type_auto_unselected"];
    NSArray<NSString *> *selectImageNameArray = @[@"ic_work_mode_type_refrigeration_selected",
                                                  @"ic_work_mode_type_dehumidify_selected",
                                                  @"ic_work_mode_type_offer_selected",
                                                  @"ic_work_mode_type_heating_selected",
                                                  @"ic_work_mode_type_auto_selected"];
    NSArray<NSString *> *titleArray = @[LC_WORK_MODE_TYPE_REFRIGERATION,
                                        LC_WORK_MODE_TYPE_DEHUMIDIFY,
                                        LC_WORK_MODE_TYPE_OFFER,
                                        LC_WORK_MODE_TYPE_HEATING,
                                        LC_WORK_MODE_TYPE_AUTO];
    NSMutableArray *array = [NSMutableArray array];
    for (NSUInteger i=0; i<5; i++) {
        ZYModeTypeModel *model = [[ZYModeTypeModel alloc] init];
        model.modeType = [[modeTypeArray objectAtIndex:i] unsignedIntegerValue];
        model.imageName = [imageNameArray objectAtIndex:i];
        model.selectImageName = [selectImageNameArray objectAtIndex:i];
        model.title = [titleArray objectAtIndex:i];
        [array addObject:model];
    }
    [self setupWithModelArray:array];
}

-(void)setupWithModelArray:(NSArray<ZYModeTypeModel *> *)modelArray{
    _dataArray = modelArray;
    _isAlterSelectView = NO;
    int count = 0;
    NSUInteger total = [modelArray count];
    CGFloat width = self.width * 0.68;
    CGFloat itemHeight = width * 0.32;
    CGFloat height = ceilf((float)total / 3) * itemHeight;
    _bgViewArray = [NSMutableArray array];
    _buttonArray = [NSMutableArray array];
    _realHeight = height;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, _realHeight)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.center = self.center;
    _bgView.centerY = self.centerY - height / 2;
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    for (ZYModeTypeModel *mode in modelArray) {
        int column = count %  3;//列
        int row = floor(count / 3);//行
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake((_bgView.width / 3) * column, row * itemHeight, _bgView.width / 3, itemHeight)];
        [bgView setUserInteractionEnabled:YES];
        [_bgView addSubview:bgView];
        [_bgViewArray addObject:bgView];
        UIButton *button = [self setupButtonWithMode:mode size:CGSizeMake(bgView.width, bgView.width)];
        [bgView addSubview:button];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
        [_buttonArray addObject:button];
        button.centerX = bgView.width / 2;
        button.centerY = bgView.height /2;
        count ++;
    }
    self.backgroundColor = [UIColor clearColor];
    self.alpha = 0;
    self.top = -self.height;
    //点击空白关闭
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(close)];
    [self addGestureRecognizer:tap];
}

-(UIButton *)setupButtonWithMode:(ZYModeTypeModel *)mode size:(CGSize)size{
    UIButton *button = [[UIButton alloc] init];
    button.size = size;
    
    [button setTarget:self action:@selector(doSlectMode:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = mode.modeType;
    [button setImage:[UIImage imageNamed:mode.imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:mode.selectImageName] forState:UIControlStateSelected];
    [button setTitleColor:RGBA(204, 204, 204, 1) forState:UIControlStateNormal];
    [button setTitleColor:kMainColor forState:UIControlStateSelected];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateNormal];
    [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateHighlighted];
    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
    return button;
}

#pragma mark - Event
-(void)doSlectMode:(UIButton *)button{
    for (UIButton *buttonItem in _buttonArray) {
        buttonItem.selected = NO;
    }
    button.selected = YES;
//    [self showOrHidden];
    if([_delegate respondsToSelector:@selector(workModeSelectView:setModeType:)]){
        [_delegate workModeSelectView:self setModeType:button.tag];
    }
}

#pragma mark - public

-(void)setupTitle{
    NSUInteger count = 0;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    for (ZYModeTypeModel *mode in _dataArray) {
        UIButton *button = [_buttonArray objectAtIndex:count];
        [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateNormal];
        [button setTitle:[language getStringByKey:mode.title] forState:UIControlStateHighlighted];
        count ++;
    }
}

-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        if(self.isAlterSelectView){
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            [window addSubview:self];
        }else{
            [self removeFromSuperview];
        }
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)setWorkModeType:(ZYWorkModeType)workModeType{
    _workModeType = workModeType;
    for (UIButton *buttonItem in _buttonArray) {
        buttonItem.selected = (buttonItem.tag == workModeType);
    }
}

@end
